package com.cola.service;

import com.cola.dao.MepDao;
import com.cola.entity.mep.Mep;
import com.cola.entity.order.Order;
import com.cola.entity.order.OrderDetail;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 经销商业务逻辑类
 * Created by Tony on 2017/8/30.
 */
public class MepService {
    private static volatile MepService instance = null;
    public static MepService getInstance() {
        if (instance == null) {
            synchronized (MepService.class) {
                if (instance == null) {
                    instance = new MepService();
                }
            }
        }
        return instance;
    }
    /**
     * 获取区域内的经销商信息
     * @param area
     * @return
     */
    public List<Map<String, Object>> getMepInfoByArea(String area){
        return MepDao.getInstance().getMepInfoByArea(area);
    }

    /**
     * 获取区域内的经销商信息
     * @param cityNo
     * @return
     */
    public List<Mep> getMepInfoByCityNo(String cityNo){
        return MepDao.getInstance().getMepInfoByCityNo(cityNo);
    }

    /**
     * 获取推荐经销商列表
     * @param customerNo
     * @return
     */
    public List<Mep> getRecommendMepByCustomerNo(String customerNo){
        return MepDao.getInstance().getRecommendMepByCustomerNo(customerNo);
    }


    /**
     * 根据经销商编号获取经销商信息
     * @param mepNo
     * @return Mep
     * @author Max
     * @since 2017/9/17
     */
    public Mep getMeoInfoByMepNo(String mepNo){
        return MepDao.getInstance().getMepInfoByMepNo(mepNo);
    }


    /**
     * 分配配送员
     * @param orderNo 订单编号
     * @param courierId 配送员id
     * @return true 成功;false 失败
     * @author Max
     * @since 2017/9/19
     */
    public boolean setCourier(String orderNo,Integer courierId){
        return MepDao.getInstance().setCourier(orderNo,courierId);
    }

    /**
     * 拒单
     * @param orderNo 订单号
     * @param mepNo 经销商编号
     * @param reason 拒单原因
     * @param messageId 消息id
     * @return true 成功;false 失败
     * @author Max
     * @since 2017/9/19
     */
    public boolean rejectOrder(String orderNo,String mepNo,String reason,Integer messageId){
        return MepDao.getInstance().rejectOrder(orderNo,mepNo,reason,messageId);
    }

    /**
     * 接单
     * @param orderNo 订单号
     * @param mepNo 经销商编号
     * @return true 成功;false 失败
     * @author Max
     * @since 2017/9/19
     */
    public boolean acceptOrder(String orderNo,String mepNo,Integer messageId){
        return MepDao.getInstance().acceptOrder(orderNo,mepNo,messageId);
    }


    /**
     * 获取订单列表
     * @param mepNo 经销商编号
     * @return List<Order>
     * @author Max
     * @since 2017/9/17
     */
    public List<Order> getMepAllOrderList(String mepNo){

        List<Order> orderList = MepDao.getInstance().getMepOrderList(mepNo);

        List<OrderDetail> orderDetails = MepDao.getInstance().getMepOrderDetailList(mepNo);

        List<OrderDetail> tempList = new ArrayList<OrderDetail>();
        for(Order order : orderList){
            tempList = new ArrayList<OrderDetail>();
            for (int i = 0; i<orderDetails.size();i++){
                if(orderDetails.get(i).getOrder_no().equals(order.getOrder_no())){
                    tempList.add(orderDetails.get(i));
                    order.setOrder_details(tempList);
                }
            }
        }
        return orderList;
    }


}
